export default class Sunshine {
  constructor(basicCesium) {
    this.basicCesium = basicCesium
    this.viewer = basicCesium.viewer
    this.clock = basicCesium.viewer.clock;
    this.primitives = basicCesium.viewer.scene.primitives;

    this.stopTime = null;
    this.initTiles();
    this.initTime();
    this.startPlay()
  }

  initTiles() {
    //开启深度检测
    this.viewer.scene.globe.depthTestAgainstTerrain = true;
    let tileset = this.primitives.add(new Cesium.Cesium3DTileset({
      url: 'tiles/tileset.json'
    }));
    tileset.readyPromise.then((tileset) => {
      this.primitives.add(tileset);
      this.viewer.zoomTo(tileset, new Cesium.HeadingPitchRange(0.5, -0.2, tileset.boundingSphere.radius * 1.0));
    })
  }

  initTime() {
    let day = Cesium.JulianDate.fromDate(new Date('2022-10-01'));
    this.start = Cesium.JulianDate.addHours(day, 0, new Cesium.JulianDate());
    this.stop = Cesium.JulianDate.addHours(day, 20, new Cesium.JulianDate());
    this.clock.startTime = this.start;
    this.clock.currentTime = this.start;
    this.clock.stopTime = this.stop;

    this.clock.clockRange = Cesium.ClockRange.LOOP_STOP;
    this.clock.clockStep = Cesium.ClockStep.SYSTEM_CLOCK_MULTIPLIER;
    this.clock.multiplier = 1600;
    this.viewer.timeline.zoomTo(this.start, this.stop);
    this.clock.shouldAnimate = false
  }

  startPlay() {
    if (this.clock.shouldAnimate = !0, this.stopTime) {
      this.clock.currentTime = this.stopTime;
    } else {
      this.stopTime = this.clock.currentTime;
      this.viewer.scene.globe.enableLighting = true;
      this.viewer.shadows = true;
    }
  }

  stopPlay() {
    if (this.stopTime) {
      this.stopTime = this.clock.currentTime;
      this.clock.shouldAnimate = false;
    }
  }
}